# -*-coding:utf-8-*-

from typing import Union, List
import numpy as np


# QAKDVSRMRSVLISLKQAPLVH
# QAKDVSRMRSVLISLKQAPLVH---------

# 1dp

def PSSM(
        sequenece: str,
        pssmfile: str
) -> List:
    '''
    计算PSSM
    https://blog.csdn.net/zjy1125/article/details/116976681?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-3.base&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-3.base
    :param sequenece:string 序列
    :param pssmfile:string 序列
    :return:
    '''
    encodings = []
    with open(pssmfile) as f:
        records = f.readlines()[3:-6]
    proteinSeq = ''
    pssmMatrix = []
    for line in records:
        array = line.strip().split()
        pssmMatrix.append(array[2:22])
        if len(array) > 1:
            proteinSeq = proteinSeq + array[1]
    # sequenece = sequenece.replace('-', '')
    # pos = proteinSeq.find(sequenece)
    tempnum = 0
    for p in range(0, len(sequenece)):
        if sequenece[p] == '-':
            encodings = encodings + ['0'] * 20
        else:
            encodings = encodings + pssmMatrix[tempnum]
            tempnum = tempnum + 1
    # print(pos)
    return encodings


def one_PSSM(data, windows=15):
    data = data
    length = len(data)
    klist = [0, 1, 2, 3]
    data_X = np.zeros((length, (2*windows+1), 20))
    data_Y = []
    for i in range(length):
        x = data[i].split(',')
        data_Y.append(int(x[4]))
        data_temp = np.array(PSSM(x[5], 'getdata/taipssm/' + x[0] + '.pssm'))
        # for j in range(len(klist)):
        #     aaindex = np.array(PSSM(x[5],j))
        data_X[i] = data_temp.reshape((2*windows+1),20)
    data_Y = np.array(data_Y)
    return data_X, data_Y


if __name__ == '__main__':

    f_r_train = open('hit-40/totalsum.csv')
    train_data = f_r_train.readlines()
    result = one_PSSM(train_data)
    print(result.shape)
